package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestBody;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpRetryException;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.PortUnreachableException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLKeyException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException;
import o.dbo.b;
import o.fus.a;
import o.fuu.e;

public class dbq {
    static fuv ˎ;
    private ftz ˊ;

    public boolean ˋ(@NonNull Map<String, File> map, @NonNull dbw o_dbw) {
        return ॱ(map, o_dbw, null);
    }

    public boolean ॱ(@NonNull Map<String, File> map, @NonNull dbw o_dbw, @Nullable b bVar) {
        Closeable closeable;
        String str;
        Object obj;
        if (o_dbw == null) {
            dbk.ˏ.ˏ("FileUpload", "uploadFileStream failed, param is null");
            return false;
        }
        dbk.ˏ.ˋ("FileUpload", "start uploadFileStream");
        dmz o_dmz = new dmz();
        Map ˋ = o_dbw.ˋ();
        String uuid = UUID.randomUUID().toString();
        String ॱ = o_dbw.ॱ();
        List list = null;
        String str2 = null;
        Closeable closeable2 = null;
        while (true) {
            try {
                break;
            } catch (Exception e) {
                closeable = closeable2;
                dbk.ˏ.ˊ("FileUpload", "FileUpload error:" + e.toString() + ", url = " + ॱ);
                if ((e instanceof UnknownHostException) || (e instanceof ConnectException) || (e instanceof SocketException) || (e instanceof PortUnreachableException) || (e instanceof SocketTimeoutException) || (e instanceof HttpRetryException) || (e instanceof NoRouteToHostException) || (e instanceof UnknownServiceException) || (e instanceof ProtocolException) || (e instanceof SSLException) || (e instanceof SSLKeyException) || (e instanceof SSLPeerUnverifiedException) || (e instanceof SSLProtocolException) || (e instanceof SSLHandshakeException)) {
                    List ˊ;
                    if (list == null) {
                        ˊ = dmv.ˊ(ॱ, 3000, e);
                    } else {
                        ˊ = list;
                    }
                    if (ˊ.size() > 0) {
                        str2 = ˊ(ॱ);
                        list = ˊ;
                        str = (String) ˊ.remove(0);
                        obj = 1;
                    } else {
                        obj = null;
                        list = ˊ;
                        str = ॱ;
                    }
                } else {
                    obj = null;
                    str = ॱ;
                }
                ˏ(o_dmz);
                dnw.ˎ(closeable);
                if (obj == null) {
                    return false;
                }
                ॱ = str;
                closeable2 = closeable;
            } catch (Throwable th) {
                th = th;
                closeable2 = closeable;
            }
        }
        fuv ˊ2 = ˊ();
        e eVar = new e();
        eVar.ˏ(fuu.ˋ);
        ˏ(eVar, ˋ, map);
        a ˋ2 = new a().ˎ(ॱ).ˋ(new dbo(eVar.ˎ(), bVar));
        ˋ2.ˋ("Charsert", "UTF-8");
        ˋ2.ˋ(HttpContants.KEY_CONTENT_TYPE, "multipart/form-data;boundary=" + uuid);
        if (!dof.ˋ(str2)) {
            ˋ2.ˋ("Host", str2);
        }
        this.ˊ = ˊ2.ˎ(ˋ2.ॱ());
        closeable2 = this.ˊ.execute();
        dbk.ˏ.ˎ("FileUpload", "responseCode =" + closeable2.ˊ());
        o_dmz.ˋ(closeable2.ᐝ().ˎ());
        o_dmz.ˋ(new InputStreamReader(o_dmz.ˋ(), "UTF-8"));
        o_dmz.ˎ(new BufferedReader(o_dmz.ˊ()));
        boolean ॱ2 = o_dbw.ॱ(doa.ˊ(o_dmz.ˏ(), 1048576));
        ˏ(o_dmz);
        dnw.ˎ(closeable2);
        return ॱ2;
        ˏ(o_dmz);
        dnw.ˎ(closeable2);
        throw th;
    }

    public static fuv ˊ() {
        if (ˎ == null) {
            dmw o_dmw = new dmw();
            o_dmw.ˋ(2);
            o_dmw.ˎ(2);
            o_dmw.ˊ(2);
            o_dmw.ˏ(2);
            o_dmw.ʻ(15);
            o_dmw.ʼ(180);
            o_dmw.ᐝ(15);
            o_dmw.ʽ();
            ˎ = dnb.ˊ(o_dmw).ˋ();
        }
        return ˎ;
    }

    private void ˏ(e eVar, Map<String, String> map, Map<String, File> map2) throws IOException {
        dbk.ˏ.ˎ("FileUpload", "setupFormBuilder");
        for (Entry entry : map.entrySet()) {
            String str = (String) entry.getKey();
            String[] strArr = new String[]{"Content-Disposition", "form-data; name=\"" + str + "\"", "Content-Transfer-Encoding", "8bit"};
            eVar.ˎ(fui.ˋ(strArr), fuw.create(fup.ॱ(RequestBody.DEFAULT_CONTENT_TYPE), (String) entry.getValue()));
        }
        if (map2.size() > 0) {
            for (Entry entry2 : map2.entrySet()) {
                str = (String) entry2.getKey();
                File file = (File) entry2.getValue();
                if (file.exists()) {
                    String canonicalPath = file.getCanonicalPath();
                    eVar.ˎ(fui.ˋ(new String[]{"Content-Disposition", "form-data; name=\"" + str + "\"; filename=\"" + dof.ˎ(canonicalPath) + "\""}), fuu.create(fup.ॱ("multipart/form-data; charset=UTF-8"), file));
                } else {
                    dbk.ˏ.ˎ("FileUpload", "file is not exists.");
                }
            }
            return;
        }
        dbk.ˏ.ˎ("FileUpload", "File is Empty!");
    }

    public void ˏ() {
        dbk.ˏ.ˎ("FileUpload", "upload cancelled");
        if (this.ˊ != null && !this.ˊ.isCanceled()) {
            this.ˊ.cancel();
        }
    }

    private String ˊ(String str) {
        String str2 = null;
        if (!dof.ˋ(str)) {
            try {
                str2 = new URL(str).getHost();
            } catch (MalformedURLException e) {
                dbk.ˏ.ˊ("FileUpload", "getHost MalformedURLException:" + e.toString());
            }
        }
        return str2;
    }

    private void ˏ(dmz o_dmz) {
        dnw.ˎ(o_dmz.ˋ());
        dnw.ˎ(o_dmz.ˊ());
        dnw.ˎ(o_dmz.ॱ());
        dnw.ˎ(o_dmz.ˎ());
        dnw.ˎ(o_dmz.ˏ());
        dnw.ˎ(o_dmz.ˏ());
    }
}
